
package com.rt.schedulenew.service.impl;

import com.rt.schedulebase.dto.DictSysparamsDto;
import com.rt.schedulebase.dto.RuleDto;
import com.rt.schedulebase.mapper.DictSysparamsMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CacheServiceImpl {
    private Logger log = LoggerFactory.getLogger(CacheServiceImpl.class);
    @Autowired
    private DictSysparamsMapper dictSysparamsMapper;
    @Autowired
    private DictScheduleApmRegularServiceImpl dictScheduleApmRegularServiceImpl;

    @Cacheable(value = {"regularCache"}, key = "#regularId")
    public RuleDto getRegularById(String regularId) {
        if (StringUtils.isBlank(regularId)) {
            return null;
        }
        log.info("----------------缓存regularCache[regularId:{}]-------------------", regularId);
        return dictScheduleApmRegularServiceImpl.getRuleDto(regularId);
    }

    @CacheEvict(value = {"regularCache"}, key = "#regularId")
    public void clearRegularCache(String regularId) {
    }

    @Cacheable(value = {"configCache"}, key = "#paramName")
    public String getConfigByName(String paramName) {
        if (StringUtils.isBlank(paramName)) {
            return null;
        }
        log.info("----------------缓存configCache[paramName:{}]-------------------", paramName);
        String paramValue = "";
        DictSysparamsDto dsDto = new DictSysparamsDto();
        dsDto.setParamName(paramName);
        List<DictSysparamsDto> dictSysparamsList = dictSysparamsMapper.selectSysparmsInfoList(dsDto);
        if (CollectionUtils.isNotEmpty(dictSysparamsList)) {
            paramValue = dictSysparamsList.get(0).getParamValue();
            if (StringUtils.isBlank(paramValue)) {
                paramValue = dictSysparamsList.get(0).getDataValue();
            }
        }
        return paramValue;
    }

    @CacheEvict(value = {"configCache"}, key = "#paramName")
    public void clearConfigCache(String paramName) {
    }
}
